var editToolBar = Class.create();

editToolBar.prototype = {

	editContainer:		false,
	parentContainer:	false,
	imagePath:			false,
	
	editMode:			'Markdown',

	buttons:			new Array(
		{
			title:		'Bold',
			file:		'text_bold',
			callBack:	'bold'
		},
		
		{
			title:		'Italic',
			file:		'text_italic',
			callBack:	'italic'
		},
		
		{
			title:		'Blockquote',
			file:		'comment',
			callBack:	'blockquote'
		},
		
		{
			title:		'Bullets',
			file:		'text_list_bullets',
			callBack:	'bullets'
		},
		
		{
			title:		'Numbered List',
			file:		'text_list_numbers',
			callBack:	'ordered'
		},	
		
		{
			title:		'Hyperlink',
			file:		'world_link',
			callBack:	'link'
		},
		
		{
			title:		'Heading 1',
			file:		'text_heading_1',
			callBack:	'heading1'
		},
		
		{
			title:		'Heading 2',
			file:		'text_heading_2',
			callBack:	'heading2'
		},
		
		{
			title:		'Heading 3',
			file:		'text_heading_3',
			callBack:	'heading3'
		}
		
	),

	initialize: function() {},
	
	create: function( toolbar, container ) {

		if( EDITMODE ) {
			this.editMode = EDITMODE;
		}
		
		this.parentContainer = $(toolbar);
		
		this.imagePath = BASEURL.replace(
			new RegExp( 'index.php' ),
			'images/admin/toolbar/'
		);
		
		var div = document.createElement( 'div' );
		div.className = 'toolBar';
		
		
		var i=0;
		while( this.buttons[i] ) {
		
			var button = document.createElement( 'span' );
			
			button.style.margin = '1em';
			button.className = 'e_toolBarButton';
			button.title = this.buttons[i].title;
			button.setAttribute( 'for', container );
			button.onclick = new Function('ToolBar.'+
				this.buttons[i].callBack+'( "'+
				container+'");');
			
			var image = document.createElement( 'img' );
			
			button.style.border = 0;
			
			image.src = this.imagePath+this.buttons[i].file+'.png';
			
			button.appendChild( image );
			
			div.appendChild( button );
		
			i++;
		}
		
		
		this.parentContainer.appendChild( div );
		
	},
	
	insertAtSelection: function( container, text ) {
		var start = $(container).selectionStart;
		
		var value = $(container).value;
		
		$(container).value = value.substring( 0, start )+
			text+
			value.substring( start+1, value.length );

	},
	
	wrapSelection: function( container, pre, post ) {
		var start = $(container).selectionStart;
		var end = $(container).selectionEnd;
		
		var value = $(container).value
		var selection = value.substr( start, (end-start) );
		
		$(container).value = value.substring( 0, start )+
			pre+selection+post+
			value.substring( end, value.length );
	},
	
	/// style function callbacks
	
	bold: function( container ) {
		if( this.editMode == 'Textile' ) {
			this.wrapSelection( container, '*', '*' );
		} else {
			this.wrapSelection( container, '**', '**' );
		}
	},
	
	italic: function( container ) {
		this.wrapSelection( container, '_', '_' );
	},
	
	blockquote: function( container ) {
		this.insertAtSelection( container, "\n> \n> \n" );
	},
	
	bullets: function( container ) {
		this.insertAtSelection( container,
			"\n* Item One\n* Item Two\n* Item Three\n\n" );
	},
	
	ordered: function( container ) {
		this.insertAtSelection( container,
			"\n1. Item One\n2. Item Two\n3. Item Three\n\n" );
	},
	
	link: function( container ) {
		var link = prompt( "Please enter the site's URL:" ) || 'http://';
		
		if( this.editMode == 'Textile' ) {
			this.wrapSelection( container, '"', '":'+link );
		} else {
			this.wrapSelection( container, '[', ']('+link+')' );
		}
	},
	
	heading1: function( container ) {
		
		if( this.editMode == 'Textile' ) {
			this.wrapSelection( container, "\nh1. ", "\n\n" );
		} else {
			this.wrapSelection( container, "\n# ", "\n\n" );
		}
	},
	
	heading2: function( container ) {

		if( this.editMode == 'Textile' ) {
			this.wrapSelection( container, "\nh2. ", "\n\n" );
		} else {
			this.wrapSelection( container, "\n## ", "\n\n" );
		}
	},
	
	heading3: function( container ) {

		if( this.editMode == 'Textile' ) {
			this.wrapSelection( container, "\nh3. ", "\n\n" );
		} else {
			this.wrapSelection( container, "\n### ", "\n\n" );
		}
	}

};

var ToolBar = new editToolBar();